package cn.mickey.exchange.service;

import cn.mickey.exchange.domain.model.OrderAddressModel;
import cn.mickey.exchange.mapper.OrderAddressMapper;
import cn.mickey.exchange.mapper.OrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderAddressService extends ServiceImpl<OrderAddressMapper,OrderAddressModel> {

    @Resource
    private OrderMapper orderMapper;
    /**
     * 为订单新增地址信息
     * @param orderAddressModel
     * @return
     */
    public boolean addOrderAddress(OrderAddressModel orderAddressModel){
        return baseMapper.insert(orderAddressModel)==1;
    }

    /**
     * 更新订单的地址信息，未验证用户身份
     * @param orderAddressModel
     * @return
     */
    public boolean updateOrderAddress(OrderAddressModel orderAddressModel){
        OrderAddressModel newOrderAddress = new OrderAddressModel();
        newOrderAddress.setOrderId(null)
                .setConsigneeName(orderAddressModel.getConsigneeName())
                .setConsigneePhone(orderAddressModel.getConsigneePhone())
                .setDetailAddress(orderAddressModel.getDetailAddress());
        return baseMapper.update(newOrderAddress, new LambdaUpdateWrapper<OrderAddressModel>()
                .eq(OrderAddressModel::getOrderAddressId,orderAddressModel.getOrderAddressId())) == 1;
    }

    /**
     * 获取订单的地址信息
     * orderId建索引
     * @param orderId
     * @return
     */
    public OrderAddressModel getOrderAddress(Long orderId){
        return baseMapper.selectOne(new LambdaQueryWrapper<OrderAddressModel>()
                .eq(OrderAddressModel::getOrderId,orderId));
    }

}
